package com.xja.food.controller;

import com.xja.food.entity.Food;
import com.xja.food.service.FoodService;
import ch.qos.logback.core.util.FileUtil;
import com.xja.food.entity.Type;

import com.xja.food.service.TypeService;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.lang.ref.PhantomReference;
import java.util.logging.Filter;

@Controller
@RequestMapping("food")
public class FoodController {
    Logger logger = LoggerFactory.getLogger(FoodController.class);
    @Reference(url = "dubbo://localhost:20882")
    private FoodService foodService;
    @Reference(url = "dubbo://localhost:20882")
    private TypeService typeService;

    @RequestMapping("foodlist")
    public String getFoodList(Model model) {
        logger.info("查询全部方法");
        List<Food> foodList = foodService.foodList();
        model.addAttribute("foodList", foodList);
        return "food_list";
    }

    @RequestMapping("add")
    public String addFood(Model model) {
        logger.info("查询类型并并返回");
        List<Type> typeList = typeService.typeList();
        model.addAttribute("typeList", typeList);
        return "food_add";
    }

    @RequestMapping("addfood")
    public String addFood(Food food, MultipartFile file) {
        logger.info("添加美食信息");
        if (file != null && file.getSize() > 0) {
            String submittedFileName = file.getOriginalFilename();
            String fileName = com.xja.food.entity.util.FileUtil.getFileName(submittedFileName);
            String realPath = "D:\\workspace\\food_xiangmu\\food_web\\src\\main\\resources\\static";
            String datePath = com.xja.food.entity.util.FileUtil.getFileDatePath();
            String filePath = realPath + "/upload/" + datePath;
            File file1 = new File(filePath);
            if (!file1.exists()) {
                file1.mkdirs();
            }
            if (!file.isEmpty()) {
                try {
                    FileOutputStream fos = new FileOutputStream(filePath + "/" + fileName);
                    InputStream in = file.getInputStream();
                    int b = 0;
                    while ((b = in.read()) != -1) {
                        fos.write(b);
                    }
                    fos.close();
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                String imgurl_new = "upload/" + datePath + "/" + fileName;
                food.setFimage(imgurl_new);
                foodService.addFood(food);
            }
        }
        return "redirect:/food/foodlist";
    }

    @RequestMapping("toUpdate")
    public String toUpdate(Model model, Integer fid) {
        logger.info("跳转到修改页面");
        Food food = foodService.selectFoodById(fid);

        model.addAttribute("food", food);
        List<Type> typeList = typeService.typeList();


        model.addAttribute("typeList", typeList);
        return "food_update";
    }

    @RequestMapping("updatefood")
    public String updatefood(Food food, MultipartFile file) {
        logger.info("修改信息");
        if (file != null && file.getSize() > 0) {
            String submittedFileName = file.getOriginalFilename();
            String fileName = com.xja.food.entity.util.FileUtil.getFileName(submittedFileName);
            String realPath = "D:\\workspace\\food_xiangmu\\food_web\\src\\main\\resources\\static";
            String datePath = com.xja.food.entity.util.FileUtil.getFileDatePath();
            String filePath = realPath + "/upload/" + datePath;
            File file1 = new File(filePath);
            if (!file1.exists()) {
                file1.mkdirs();
            }
            if (!file.isEmpty()) {
                try {
                    FileOutputStream fos = new FileOutputStream(filePath + "/" + fileName);
                    InputStream in = file.getInputStream();
                    int b = 0;
                    while ((b = in.read()) != -1) {
                        fos.write(b);
                    }
                    fos.close();
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                String imgurl_new = "upload/" + datePath + "/" + fileName;
                food.setFimage(imgurl_new);
            }

            foodService.updatefood(food);
        }

        return "redirect:/food/foodlist";
    }

    @RequestMapping("deletefood")
    public String deleteFood(Integer fid) {
        logger.info("删除信息");
        foodService.deleteFood(fid);
        return "redirect:/food/foodlist";
    }

    @RequestMapping("foodPuList")
    public String foodPuList(Model model) {
        logger.info("查询全部方法");
        List<Food> foodList = foodService.foodList();
        model.addAttribute("foodList", foodList);
        return "food_pulist";
    }

}
